<!-- 课程管理(模块) => 课程列表(菜单) => 台账操作列(课代表管理按钮) => 点开 => 选择课代表按钮 => 点开后展示此组件 -->
<template>
  <Dialog :title="t('assistant.addBtn')" v-model="dialogVisible" width="800">
    <ContentWrap>
      <el-alert class="!mb-[10px]" type="error" :closable="false">
        {{ t('course.index.selectAssistantTip') }}
      </el-alert>
      <vxe-table
        id="mentor-select-table"
        ref="tableRef"
        v-loading="loading"
        :data="list"
        stripe
        :column-config="{ resizable: true }"
        :size="vxeTableSize"
      >
        <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
        <vxe-column
          :title="t('assistant.fields.username')"
          field="username"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('assistant.fields.nickname')"
          field="nickname"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('systemUser.fields.mobile')"
          field="mobile"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('common.operate')"
          field="operator"
          align="center"
          show-header-overflow
          show-overflow
          fixed="right"
          width="80"
        >
          <template #default="scope">
            <div class="flex items-center justify-center">
              <el-button type="primary" link @click="handleChoose(scope.row)">
                <Icon icon="ep:check" />
                {{ t('action.choose') }}
              </el-button>
            </div>
          </template>
        </vxe-column>
      </vxe-table>

      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </Dialog>
</template>

<script setup lang="ts">
import * as UserApi from '@/api/system/user'
import { UserVO } from '@/api/system/user'
import { useAppStore } from '@/store/modules/app'
import { CourseAdminRespVO } from '@/api/course/course'
import { AssistantApi, AssistantVO } from '@/api/course/assistant'

/** 课程分配教师 列表 */
defineOptions({ name: 'CourseAssistantSelect' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false)

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  subUser: true, // 查询子账号
  roleId: 163, // 163固定为课代表
  username: undefined,
  nickname: undefined,
  mobile: undefined,
  jobCode: undefined,
  status: undefined,
  deptId: undefined,
  createTime: []
})

const queryFormRef = ref() // 搜索的表单

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UserApi.getUserPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

const handleChoose = async (row: UserVO) => {
  try {
    await message.confirm(t('assistant.confirm'))
  } catch {
    return
  }
  loading.value = true
  try {
    const data: Partial<AssistantVO> = {
      courseId: course.value.id,
      semesterId: course.value.semesterId,
      assistantId: row.id
    }
    await AssistantApi.addToCourse(data)
    message.success(t('common.operSuccess'))
    dialogVisible.value = false
    emits('choose')
  } finally {
    loading.value = false
  }
}

const course = ref<CourseAdminRespVO>({} as CourseAdminRespVO)
const open = async (row: CourseAdminRespVO) => {
  course.value = row
  dialogVisible.value = true
  // 查询所有有教师角色的人
  getList()
}

const emits = defineEmits(['choose'])

defineExpose({ open })
</script>

<style scoped lang="scss"></style>
